﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Ko2.Portal.SP_List
{

    public static class SPListItemUserHelper
    {

        /// <summary>
        /// Returns the login name of an User-Field.
        /// </summary>
        public static string GetFieldValueUserLogin(this SPListItem item, string fieldName)
        {
            if (item != null)
            {
                SPFieldUserValue userValue =
                  new SPFieldUserValue(
                    item.Web, item[fieldName] as string);
                return userValue.User.LoginName;
            }
            else
            {
                return string.Empty;
            }
        }

        public static void GetSPUser_or_SPGroup(this SPListItem item, string fieldName, ref SPUser parUser, ref SPGroup parGroup, ref SPFieldUserValueCollection parValueCollection)
        {
            parUser = null;
            parGroup = null;

            int id = -1;
            string name = "";
            try
            {
                if (SPSecurityHelper.GetGroupByString(item[fieldName].ToString(), ref id, ref name))
                {
                    parGroup = SPSecurityHelper.GetGroup(item.Web, name);
                    if (parGroup == null)
                        parUser = SPSecurityHelper.GetUserByName(item.Web, name, SPSecurityHelper.SPUserNameType.Name);
                }
            }
            catch
            {
            }

            /*
            // http://ilkercakir.blogspot.com/2011/01/how-to-get-spuser-or-spgroup-from.html
            SPField field = item.Fields[fieldName] as SPField;
            //SPPrincipal principal = (SPFieldUserValue)item[fieldName];

            if (item[fieldName] is SPFieldUserValue)
                userOrGroupValue = (SPFieldUserValue)item[fieldName];
            else
            {
                if (item[fieldName] is SPFieldUserValue)
                    userOrGroupValue = (SPFieldUserValue)item[fieldName];
                if (item[fieldName] is SPFieldUserValueCollection)
                {
                    parValueCollection = (SPFieldUserValueCollection)item[fieldName];
                    //if (parValueCollection.Count > 0)
                    //    userOrGroupValue = spfieldUserValCol[0];
                }
            }

            //if (userOrGroupValue == null) 
            //    throw new SPException("somw error");

            if (userOrGroupValue != null)
            {
                // If User value is null it's a Group, if not User
                if (userOrGroupValue.User != null)
                {
                    parUser = userOrGroupValue.User;
                }
                else
                {
                    parGroup = item.Web.Groups[userOrGroupValue.LookupValue];
                }
            }
            */ 
        }

        /// <summary>
        /// Sets the value of a User-Field to a login name.
        /// </summary>
        public static void SetFieldValueUser(this SPListItem item, string fieldName, string loginName)
        {
            if (item != null)
            {
                item[fieldName] = item.Web.EnsureUser(loginName);
            }
        }

        /// <summary>
        /// Sets the value of a User-Field to an SPPrincipal
        /// (SPGroup or SPUser).
        /// </summary>
        public static void SetFieldValueUser(this SPListItem item, string fieldName, SPPrincipal principal)
        {
            if (item != null)
            {
                item[fieldName] = principal;
            }
        }

        public static void SetFieldValueUser(this SPListItem item, string fieldName, IEnumerable<SPPrincipal> principals)
        {
            if (item != null)
            {
                SPFieldUserValueCollection fieldValues =
                  new SPFieldUserValueCollection();

                foreach (SPPrincipal principal in principals)
                {
                    fieldValues.Add(
                      new SPFieldUserValue(
                        item.Web, principal.ID, principal.Name));
                }
                item[fieldName] = fieldValues;
            }
        }

        /// <summary>
        /// Sets the value of a multivalue User-Field to
        /// a list of user names.
        /// </summary>
        public static void SetFieldValueUser(this SPListItem item, string fieldName, IEnumerable<string> loginNames)
        {
            if (item != null)
            {
                SPFieldUserValueCollection fieldValues =
                  new SPFieldUserValueCollection();

                foreach (string loginName in loginNames)
                {
                    SPUser user = item.Web.EnsureUser(loginName);
                    fieldValues.Add(
                      new SPFieldUserValue(
                        item.Web, user.ID, user.Name));
                }

                item[fieldName] = fieldValues;
            }
        }
    } 

}
